<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            list-style: none;
        }

        .lights-box {
            display: flex;
            width: 120px;
            margin: auto;
            padding: 0px 5px;
            flex-direction: column;
            align-items: center;
            border: 1px solid #000;
            border-radius: 20px;

        }

        .lights-box>li {
            width: 100px;
            height: 100px;
            margin: 5px 0px;
            border-radius: 50%;

        }

        .turn-off {
            background-color: #ababab;
        }

        .lights-box>.turn-on:nth-child(1) {
            background-color: #f00;
        }

        .lights-box>.turn-on:nth-child(2) {
            background-color: #ff0;
            box-shadow: 0 0 10px 1px #cece47cc;
        }

        .lights-box>.turn-on:nth-child(3) {
            background-color: #0f0;
        }
    </style>
</head>

<body>
    <ul class="lights-box">
        <li class="turn-off"></li>
        <li class="turn-off turn-on"></li>
        <li class="turn-off"></li>
    </ul>
</body>
<script>
    // 转为数组对象，方便使用遍历方法
    let lis = Array.from(document.querySelectorAll(".lights-box>li"));
    let arr = [5000, 5000, 5000];
    let index = 0;

    async function start() {
        // 始循环
        for (; ;) {
            // 黄灯
            if (index == 1) {
                let time = arr[index] / 10;
                for (let i = 0; i < 10; i++) {
                    await createPromise(time, index, i % 2 == 0);
                }
            } else {
                await createPromise(arr[index], index);
            }
            index++;
            if (index >= arr.length) {
                index = 0;
            }
        }
    }

    /**
     * 通过Promise来做出“暂停”的感觉
     * time: 显示时长 
     * index: 当前显示的灯的序号
     */
    function createPromise(time, index, turnOn = true) {
        console.log(`当前第${index}灯点亮`);

        // 遍历，更改灯的打开状态
        lis.forEach((v, i) => {
            classList = v.classList;
            if (i == index && turnOn) {
                classList.add("turn-on");
            } else {
                classList.remove("turn-on");
            }
        })

        return new Promise((reslove) => {
            setTimeout(() => {
                reslove();
            }, time)
        })
    }

    // start();

</script>

</html>